<?php

namespace app\master\controller;

use think\Db;
use think\Exception;

class Drivemanage extends Base
{
    //平台驾驶员
    public function platformdrive()
    {
        if (request()->isGet()) {
            return $this->fetch('platformdrive');
        }
        $where = [
            'd.drive_type' => 1,
        ];
        $is_store = session('is_store');
        if ($is_store == 1) {
            $admin_id = session('admin_id');
            $store_id = model('Store')->getStoreId($admin_id);
            $where['d.store_id'] = $store_id;
        }
        $page = input("page");
        $limit = input("limit");
        $list = model('Drive')->platformdrive($page, $limit, $where);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //临时驾驶员
    public function temporarydrive()
    {
        if (request()->isGet()) {
            return $this->fetch('temporarydrive');
        }

        $where = [
            'd.drive_type' => 2,
        ];
        $is_store = session('is_store');
        if ($is_store == 1) {
            $admin_id = session('admin_id');
            $store_id = model('Store')->getStoreId($admin_id);
            $where['d.store_id'] = $store_id;
        }
        $page = input("page");
        $limit = input("limit");
        $list = model('drive')->temporarydrive($page, $limit, $where);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    // 添加驾驶员
    public function adddrive()
    {
        if (request()->isGet()) {
            $store = db("store")->select();
            $level = db("driving_licence")->select();
            $this->assign("store", $store);
            $this->assign("level", $level);
            return $this->fetch("adddrive");
        }
        $data = input("post.");
        $Drivemanage = new \app\common\validate\Drivemanage();
        $msg = $Drivemanage->scene("adddrive")->check($data);
        if (!$msg) $this->ajaxReturn(603, $Drivemanage->getError());
        if (!checkTelFormat($data['drive_tel'])) $this->ajaxReturn(603, '请输入正确电话号');
        $drive = [
            "drive_name" => $data['drive_name'],
            "drive_tel" => $data['drive_tel'],
            "drive_card" => $data['drive_card'],
            "drive_money" => $data['drive_money'],
            "drive_type" => $data['drive_type'],
            "drive_status" => 1,
            "driving_licence_id" => $data['drivelevel_id'],
            "drive_sex" => $data['drive_sex'],
            "drive_isgo" => 2,
            "store_id" => $data['store_id'],
        ];
        //正面
        $pathz = config("TEMPORARY_CATALOG_FILE_PATH") . getFileName($data["img"]);
        $resz = moveFile($pathz, config("JUST_FILE_PATH"));
        if (!$resz) $this->ajaxReturn(603, selectFile);
        //反面
        $pathf = config("TEMPORARY_CATALOG_FILE_PATH") . getFileName($data["image"]);
        $resf = moveFile($pathf, config("CBACK_FILE_PATH"));
        if (!$resf) $this->ajaxReturn(603, selectFile);
//        $drive['drive_img'] = $res;
        $drive['dirve_just'] = $resz;
        $drive['drive_back'] = $resf;
        $isdrive = db('drive')->insert($drive);
        if ($isdrive) {
            $this->ajaxReturn(200, "添加成功", url("Drivemanage/platformdrive"));
        } else {
            $this->ajaxReturn(603, "添加失败");
        }
    }

    //临时驾驶员审核
    public function temporarydriveapply()
    {
        $data["driveapply_status"] = input("driveapply_status");
        if (request()->isGet()) {
            $this->assign("list", $data);
            return $this->fetch('temporarydriveapply');
        }
        $page = input("page");
        $limit = input("limit");
        $where = [];
        if (!empty($data['driveapply_status'])) $where["da.driveapply_status"] = $data['driveapply_status'];
        $list = model('drive')->temporarydriveapply($page, $limit, $where);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //同意审核
    public function agreeapply()
    {
        $driveapply_id = input('driveapply_id');
        $data = db('DriveApply')->where('driveapply_id', $driveapply_id)->find();
        if ($data['driveapply_status'] != 1) $this->ajaxReturn(603, "您不可执行此操作");
        $drive = [
            "drive_name" => $data['driveapply_name'],
            "drive_img" => $data['driveapply_img'],
            "drive_tel" => $data['driveapply_tel'],
            "drive_card" => $data['driveapply_card'],
            "drive_money" => $data['driveapply_money'],
            "dirve_just" => $data['dirveapply_just'],
            "drive_back" => $data['driveapply_back'],
            "drive_type" => 2,
            "drive_status" => 1,
            "driving_licence_id" => $data['driving_licence_id'],
            "drive_sex" => $data['driveapply_sex'],
            "drive_isgo" => 2,
            "store_id" => $data['store_id'],
            "drive_driveyear" => $data['driveapply_driveyear'],
            "drive_old" => $data['driveapply_old']
        ];
        $admin_id = session('admin_id');
        $time = time();
        Db::startTrans();
        try {
            db('drive')->insert($drive);
            db('DriveApply')->where('driveapply_id', $driveapply_id)->update(['driveapply_status' => 2, 'driveapply_auditor' => $admin_id, 'driveapply_auditortime' => $time]);
            Db::commit();
            $this->ajaxReturn(200, successMsg, url("Drivemanage/temporarydriveapply"));
        } catch (\Exception $e) {
            Db::rollback();
            $this->ajaxReturn(603, errorMsg);
        }
    }

    //拒绝审核
    public function refuseapply()
    {
        $driveapply_id = input('driveapply_id');
        $data = db('DriveApply')->where('driveapply_id', $driveapply_id)->find();
        if ($data['driveapply_status'] != 1) $this->ajaxReturn(603, "您不可执行此操作");
        $admin_id = session('admin_id');
        $time = time();
        $list = db('DriveApply')->where('driveapply_id', $driveapply_id)->update(['driveapply_status' => 3, 'driveapply_auditor' => $admin_id, 'driveapply_auditortime' => $time]);
        if ($list) {
            $this->ajaxReturn(200, successMsg, url("Drivemanage/temporarydriveapply"));
        } else {
            $this->ajaxReturn(603, errorMsg);
        }
    }

    //驾驶员事务申请
    public function drivething()
    {
        $data["drivething_status"] = input("drivething_status");
        if (request()->isGet()) {
            $this->assign("list", $data);
            return $this->fetch('drivething');
        }
        $page = input("page");
        $limit = input("limit");
        $where = [];
        $is_store = session('is_store');
        if ($is_store == 1) {
            $admin_id = session('admin_id');
            $store_id = model('Store')->getStoreId($admin_id);
            $where['dr.store_id'] = $store_id;
        }
        if (!empty($data['drivething_status'])) $where["d.drivething_status"] = $data['drivething_status'];
        $list = model('drive')->drivething($page, $limit, $where);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //同意申请（驾驶员事物）
    public function agreedriveapply()
    {
        $drivething_id = input('drivething_id');
        $data = db('drivething')->where('drivething_id', $drivething_id)->find();
        if ($data['drivething_status'] != 1) $this->ajaxReturn(603, "您不可执行此操作");
        $admin_id = session('admin_id');
        $time = time();
        if ($data['drivething_type'] == 1) {
            $status = 3;
        } elseif ($data['drivething_type'] == 2) {
            $status = 5;
        } elseif ($data['drivething_type'] == 3) {
            $status = 6;
        } elseif ($data['drivething_type'] == 4) {
            $status = 8;
        } elseif ($data['drivething_type'] == 5) {
            $status = 7;
        } elseif ($data['drivething_type'] == 6) {
            $status = 7;
        }
        Db::startTrans();
        try {
            db('drivething')->where('drivething_id', $drivething_id)->update(['drivething_status' => 2, 'drivething_auditor' => $admin_id, 'drivething_auditortime' => $time]);
            db('drive')->where('drive_id', $data['drive_id'])->update(['drive_status' => $status]);
            Db::commit();
            $this->ajaxReturn(200, successMsg, url("Drivemanage/drivething"));
        } catch (\Exception $e) {
            Db::rollback();
            $this->ajaxReturn(603, errorMsg);
        }
    }

    //拒绝申请（驾驶员事物）
    public function refusedriveapply()
    {
        $drivething_id = input('drivething_id');
        $data = db('drivething')->where('drivething_id', $drivething_id)->find();
        if ($data['drivething_status'] != 1) $this->ajaxReturn(603, "您不可执行此操作");
        $admin_id = session('admin_id');
        $time = time();
        $list = db('drivething')->where('drivething_id', $drivething_id)->update(['drivething_status' => 3, 'drivething_auditor' => $admin_id, 'drivething_auditortime' => $time]);
        if ($list) {
            $this->ajaxReturn(200, successMsg, url("Drivemanage/drivething"));
        } else {
            $this->ajaxReturn(603, errorMsg);
        }
    }

    //驾驶员工作记录
    public function driveWorkLog()
    {
        $id = input('id');
        $type = input('type');//驾驶员类型
        if (request()->isGet()) {
            $this->assign('id', $id);
            $this->assign('type', $type);
            return $this->fetch('driveworklog');
        }
        $where = [
            'co.drive_id' => $id,
            'd.drive_type' => $type,
            'co.contractcar_status' => 4,
            'co.contractcar_type' => ['in', [1, 2]]
        ];
        $page = input('page');
        $limit = input('limit');
        $field = "s.store_name,d.drive_name,from_unixtime(co.contractcar_bigontime,'%Y .%m .%d %H:%i:%s') as contractcar_bigontime,from_unixtime(co.contractcar_endtime,'%Y .%m .%d %H:%i:%s') as contractcar_endtime,contractcar_company,c.car_plate,co.mileage";
        $list = model('Contractcar')->getDriveWorkLog($field, $where, $page, $limit);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //平台驾驶员签到记录
    public function signLog()
    {
        $id = input('id');
        if (request()->isGet()) {
            $this->assign('id', $id);
            return $this->fetch('signlog');
        }
        $where = [
            'sg.drive_id' => $id,
        ];
        $page = input('page');
        $limit = input('limit');
        $field = "s.store_name,d.drive_name,sg.signin_longitude,sg.signin_latitude,sg.type,from_unixtime(sg.signin_time,'%Y .%m .%d %H:%i:%s') as signin_time";
        $list = model('Signin')->getSignLog($field, $where, $page, $limit);
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k]['address'] = getAddress($list['data'][$k]['signin_longitude'], $list['data'][$k]['signin_latitude']);
        }
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }
}